// Copyright (c) 2010 SuccessFactors, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
//     * Redistributions of source code must retain the above
//       copyright notice, this list of conditions and the following
//       disclaimer.
//
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials
//       provided with the distribution.
//
//     * Neither the name of the SuccessFactors, Inc. nor the names of
//       its contributors may be used to endorse or promote products
//       derived from this software without specific prior written
//       permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.

package org.owasp.jxt;

import java.util.Iterator;

/**
 * LoopIterator
 *
 * @author Jeffrey Ichnowski
 * @version $Revision: 8 $
 */
public final class LoopIterator<T> implements LoopStatus<T>, Iterator<T> {
    private final Iterator<T> _iterator;
    private T _current;
    private int _count;
    private int _index = -1;

    LoopIterator(Iterator<T> iter) {
        _iterator = iter;
    }

    public boolean hasNext() {
        return _iterator.hasNext();
    }

    public T next() {
        _current = _iterator.next();
        _count++;
        _index++; // += _step
        return _current;
    }

    public void remove() {
        _iterator.remove();
    }

    /** 1-based inde of the iteration cycle.  E.g. it is always 1, 2,
     * 3, ... regardless of first, last, step, iterator, etc... */
    public int getCount() { return _count; }

    /** 0-based index into the collection being iterated through.  For
     * most loops this will be 0, 1, 2, ....  However if start is set
     * to, for example, 5, then this will be 5, 6, 7, ... */
    public int getIndex() { return _index; }

    /** Current object of the iteration */
    public T getCurrent() { return _current; }

    /** Returns true if this is the first element in the iteration. */
    public boolean isFirst() { return _count == 1; }

    /** Returns true if this is the last element in the iteration. */
    public boolean isLast() { return !_iterator.hasNext(); }

    /** TBD: first index of iteration. */
    public Integer getBegin() { return null; }
    /** TBD: last index of iteration. */
    public Integer getEnd() { return null; }
    /** TBD: increment of iteration. */
    public Integer getStep() { return null; }
} // LoopIterator
